/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.itschapala.dsw2.vista;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.itschapala.dsw2.biblioteca8a.bl.CategoriaBlLocal;
import mx.itschapala.dsw2.biblioteca8a.modelo.Categoria;

/**
 *
 * @author Lindemann
 */
@ManagedBean
@SessionScoped
public class CategoryBean {
    
    //---------------EJB'S-------------------------
    @EJB
    private CategoriaBlLocal categoriaBl;
    //------------------variables(propiedades)--------------
    private List<Categoria> lista;
    private Categoria category;
    private String accion;
//-----------constructor--------------------------------

    
    public CategoryBean() {
        category= new Categoria();
    }
    //-----------metodos GET y SET--------------------------------    

    public List<Categoria> getLista() {
        lista = categoriaBl.getAll();
        return lista;
    }

    public Categoria getCategory() {
        return category;
    }

    public void setCategory (Categoria category) {
        this.category = category;
    }

//-----------Acciones--------------------------------   
    public String procesarAccion() {
        if(accion.equalsIgnoreCase("Nuevo")){
        categoriaBl.register(category);
        } else if(accion.equalsIgnoreCase("Editar")){
                categoriaBl.modify(category);
            }
        else if(accion.equalsIgnoreCase("Eliminar")){
                categoriaBl.delete(category);
           
}
        accion="";
        return "CategoriaVista";
    }

//-----------Listeners--------------------------------  
    public void prepararNuevo(ActionEvent evt){//accionEvent de faces
        category=new Categoria();
        accion="Nuevo";
    }
    
    public void prepararEditar(ActionEvent evt){
        System.out.println(evt.getComponent().getClientId());
        String arr[]=evt.getComponent().getClientId().split(":");
        String num=arr[2];
        int index=Integer.parseInt(num);
        System.out.println("el numero es: "+index);
        category=lista.get(index);
        accion="Editar";
    }
    public void prepararEliminar(ActionEvent evt){
        String arr[]=evt.getComponent().getClientId().split(":");
        String num=arr[2];
        int index=Integer.parseInt(num);
        System.out.println("el numero es: "+index);
        category=lista.get(index);
        accion="Eliminar";
    }
}
